<?php namespace App\Http\Controllers\user\industry;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use Illuminate\Pagination\LengthAwarePaginator;
use Landish\Pagination\Pagination as BootstrapPresenter;

use App\Models\Projects;
use App\Models\Domains;
use App\Models\Techs;
use App\Models\Devices;
use App\Models\Technologies;
use App\Models\Users;
use App\Models\UserRoles;
use App\Models\BusinessCircle;
use App\Models\ThirdpartyIndustry;
use App\Models\ThirdpartyProfessionalList;
use App\Models\ThirdpartyHistoryProject;
use App\Models\ThirdpartyDevice;


use Input;
use App;
use Queue;
use Log;

class GroupController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		// Queue::push("App\Commands\SendSMS", ["number" => "18668190573", "msg" => "【泽大合润投资管理公司】您的验证码是13211"]);
		// Queue::push("App\Commands\SendEmail", ["to" => "751077517@qq.com", "title" => "来自SendCloud的第一封邮件！", "content" => "你太棒了！你已成功的从SendCloud发送了一封测试邮件，接下来快登录前台去完善账户信息吧！"]);

		$numPerPage = 24;
		$users = Users::where("validate_state", 2)->whereNotIn('role_id', [0,1,8,9,10]);
		if(Input::has("gid")){
			$users->where("role_id", Input::get("gid"));
		}
		$count = $users->count();

		$curPage = (int)Input::get("page", 1);
		$p = new LengthAwarePaginator($users, $count, $numPerPage, $curPage);
		$p->setPath("");
        $presenter = new BootstrapPresenter($p);

		$users = $users->skip(($curPage - 1) * $numPerPage)->take($numPerPage)->get();
		foreach($users as $user){
			switch ($user["role_id"]) {
				case env("ENERGY"):
					$user->load("poweruser", "poweruser.registProvince", "poweruser.registCity", "poweruser.registArea");
					break;
				case env("SAVER"):
					$user->load("energysaver", "energysaver.registProvince", "energysaver.registCity", "energysaver.registArea");
					break;
				case env("DEVICE_TECH"):
					$user->load("devicetech", "devicetech.registProvince", "devicetech.registCity", "devicetech.registArea");
					break;
				case env("THIRD"):
					$user->load("thirdparty", "thirdparty.registProvince", "thirdparty.registCity", "thirdparty.registArea");
					break;
				case env("ASSOCIATION"):
					$user->load("association", "association.registProvince", "association.registCity", "association.registArea");
					break;
				case env("GOVERNMENT");
					$user->load("government", "government.registProvince", "government.registCity", "government.registArea");
					break;
			}
		}

		$recUsers = Users::where("validate_state", 2)->where("is_rec", 1)->orderBy("order", "desc")->skip(($curPage - 1) * 5)->take(5)->get();
		foreach($recUsers as $user){
			switch ($user["role_id"]) {
				case env("ENERGY"):
					$user->load("poweruser");
					break;
				case env("SAVER"):
					$user->load("energysaver");
					break;
				case env("DEVICE_TECH"):
					$user->load("devicetech");
					break;
				case env("THIRD"):
					$user->load("thirdparty");
					break;
				case env("ASSOCIATION"):
					$user->load("association");
					break;
				case env("GOVERNMENT");
					$user->load("government");
					break;
			}
		}

		$roles = UserRoles::whereNotIn("id", [0,1,8,9,10])->get()->toArray();

		$bottomNumPerPage = 6;
		$bottomProject = Projects::with(["projectDomain", "projectDomain.domain", "projectDomain.domain.parent", "projectTech", "projectTech.tech", "investmentType"])->where("is_rec", 1)->where("verify_state", 1)->skip(($curPage - 1) * $bottomNumPerPage)->take(6)->get();
		$bottomDevice = Devices::where("is_rec", 1)->where("verify_state", 1)->skip(($curPage - 1) * $bottomNumPerPage)->take(6)->get();
		$bottomTech = Technologies::where("is_rec", 1)->where("verify_state", 1)->skip(($curPage - 1) * $bottomNumPerPage)->take(6)->get();

		$queryStr = "";
		foreach (Input::all() as $key => $value)
		{
			$queryStr .= $key . "=" . $value . "&";
		}
		$params = ["users" => $users->toArray(), "count" => $count, "roles" => $roles, "recommand" => $recUsers, "bottomProject" => $bottomProject, "bottomDevice" => $bottomDevice, "bottomTech" => $bottomTech, "bottomTech" => $bottomTech, "curQuery" => $queryStr];

		if(Input::has("gid")){
			$firstLevel = UserRoles::where("id", Input::get("gid"));
			if($firstLevel->count()){
				$params["firstLevel"] = $firstLevel->get()[0]["role_name"];
			}
		}

		return view('user.industry.group', $params)->withPresenter($presenter)->withIsGroup(true)->withIsIndustry(true);
	}

	public function groupDetail()
	{
		$gid = (int)Input::get("gid");
		if(!$gid){
			return App::abort(404);
		}
		$user = Users::where("validate_state", 2)->where("id", $gid);
		if(count($user)){
			$user = $user->get()[0];
			switch ($user["role_id"]) {
				case env("ENERGY"):
					$user->load("poweruser", "poweruser.registProvince", "poweruser.registCity", "poweruser.registArea");
					$user["userData"] = $user["poweruser"];
					unset($user["poweruser"]);
				break;
				case env("SAVER"):
					$user->load("energysaver", "energysaver.registProvince", "energysaver.registCity", "energysaver.registArea");
					$user["userData"] = $user["energysaver"];
					unset($user["energysaver"]);
				break;
				case env("DEVICE_TECH"):
					$user->load("devicetech", "devicetech.registProvince", "devicetech.registCity", "devicetech.registArea");
					$user["userData"] = $user["devicetech"];
					unset($user["devicetech"]);
				break;
				case env("THIRD"):
					$user->load("thirdparty", "thirdparty.registProvince", "thirdparty.registCity", "thirdparty.registArea");
					$user["userData"] = $user["thirdparty"];
					unset($user["thirdparty"]);
				break;
				case env("ASSOCIATION"):
					$user->load("association", "association.registProvince", "association.registCity", "association.registArea");
					$user["userData"] = $user["association"];
					unset($user["association"]);
				break;
				case env("GOVERNMENT"):
					$user->load("government", "government.registProvince", "government.registCity", "government.registArea");
					$user["userData"] = $user["government"];
					unset($user["government"]);
				break;
			}
			$user = $user->toArray();
		}

		// dd($user);
		return view('user.industry.group_detail', ["model" => $user])->withIsIndustry(true);
	}


	public function getProjects()
	{
		$uid = Input::get("uid");
		$data = Projects::where("uid", $uid)->where("verify_state", 1)->get()->toArray();
		return $this->json(["data" => $data]);
	}

	public function getCircle()
	{
		$uid = Input::get("uid");
		$data = BusinessCircle::with(["profile"])->where("uid", $uid)->get()->toArray();
		return $this->json(["data" => $data]);
	}

	public function getDevice()
	{
		$uid = Input::get("uid");
		$data = Devices::where("uid", $uid)->where("verify_state", 1)->get()->toArray();
		return $this->json(["data" => $data]);
	}

	public function getTech()
	{
		$uid = Input::get("uid");
		$data = Technologies::where("uid", $uid)->where("verify_state", 1)->get()->toArray();
		return $this->json(["data" => $data]);
	}

	public function getFinancingProject()
	{
		$uid = Input::get("uid");
		$data = Projects::where("uid", $uid)->where("is_financing", 1)->get()->toArray();
		return $this->json(["data" => $data]);

	}

	public function getFinancingDevicetech()
	{
		$uid = Input::get("uid");
		$device = Devices::where("uid", $uid)->where("is_financing", 1)->get()->toArray();
		$tech = Technologies::where("uid", $uid)->where("is_financing", 1)->get()->toArray();
		return $this->json(["device" => $device, "tech" => $tech]);
	}


	public function getThirdIndustry()
	{
		$uid = Input::get("uid");
		$industry_id = ThirdpartyIndustry::where("uid", $uid)->pluck("id");
		$person = ThirdpartyProfessionalList::where("industry_id", $industry_id)->get()->toArray();
		$project = ThirdpartyHistoryProject::where("industry_id", $industry_id)->get()->toArray();
		$device = ThirdpartyDevice::where("industry_id", $industry_id)->get()->toArray();

		return $this->json(["person" => $person, "project" => $project, "device" => $device]);

	}


// use App\Models\ThirdpartyProfessionalList;
// use App\Models\ThirdpartyHistoryProject;
// use App\Models\ThirdpartyDevice;

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}

	public function groupList()
	{
		return view("user.industry_group_list");
	}

}
